﻿using System;
using System.BLL;
using System.Collections.Generic;
using System.Dto;
using System.IBLL;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace System.MVC.Controllers
{
    /// <summary>
    /// 社区管理菜单控制器
    /// </summary>
    public class CommunityController : Controller
    {
        /// <summary>
        /// 消防及电梯物资信息表页面
        /// </summary>
        /// <returns></returns>
        public ActionResult MaterialList()
        {
            return View();
        }
        /// <summary>
        /// 消防及电梯物资信息表页面table数据请求接口
        /// </summary>
        /// <param name="limit">条数</param>
        /// <param name="page">页数</param>
        /// <param name="MaterialName">物资名筛选器</param>
        /// <param name="MaterialLocation">物资位置筛选器</param>
        /// <param name="MaintenanceCycle">维修日期筛选器</param>
        /// <returns></returns>
        public JsonResult GetMaterialList(int limit, int page, string MaterialName, string MaterialLocation, string MaintenanceCycle)
        {
            IMaterialManager manager = new MaterialManager();
            int datalet;
            var data = manager.GetMaterialListDtos(limit, page, out datalet, MaterialName, MaterialLocation, MaintenanceCycle);
            return Json(new { code = 0, msg = "获取成功", count = datalet, data = data }, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 消防及电梯物资信息表页面table表详情按钮数据请求接口
        /// </summary>
        /// <param name="id">物资ID</param>
        /// <returns></returns>
        public ActionResult AllMaterialMaintenance(int id)
        {
            ViewBag.id = id;
            return View();
        }
        /// <summary>
        /// 检修详情页面table数据请求接口
        /// </summary>
        /// <param name="id">物资ID</param>
        /// <param name="limit">条数</param>
        /// <param name="page">页数</param>
        /// <param name="MaintenanceResults">结果筛选器</param>
        /// <param name="MaintenanceContent">内容筛选器</param>
        /// <returns></returns>
        public JsonResult GetMaterialMaintenanceList(int id, int limit, int page, string MaintenanceResults, string MaintenanceContent)
        {
            IMaterialManager manager = new MaterialManager();
            int datalet;
            var data = manager.GetMaterialMaintenanceListById(id, limit, page, out datalet, MaintenanceResults, MaintenanceContent);
            return Json(new { code = 0, msg = "获取成功", count = datalet, data = data }, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 消防及电梯物资信息表页面table确认按钮页面请求接口 既确认检修页面
        /// </summary>
        /// <param name="id">物资ID</param>
        /// <returns></returns>
        public ActionResult ConfirmMaterial(int id)
        {
            ViewBag.id = id;
            return View();
        }
        /// <summary>
        /// 确认检修页面数据请求接口
        /// </summary>
        /// <param name="MateriaId">物资ID</param>
        /// <param name="MaintenanceResults">检修结果</param>
        /// <param name="MaintenanceContent">检修内容</param>
        /// <param name="Remark">备注</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult ConfirmMaterial(string MateriaId,string MaintenanceResults,string MaintenanceContent,string Remark)
        {
            IMaterialManager manager = new MaterialManager();
            manager.ConfirmMaterialById(MateriaId, MaintenanceResults, MaintenanceContent, Remark);
            return Json(new { code = 0, msg = "确认成功"});
        }
        /// <summary>
        /// 消防及电梯物资页面table删除按钮请求接口
        /// </summary>
        /// <param name="id">物资ID</param>
        /// <returns></returns>
        public JsonResult DelMaterial(int id)
        {
            IMaterialManager manager = new MaterialManager();
            manager.DelMaterialById(id);
            return Json(new { code = 0, msg = "删除成功"}, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 消防及电梯物资页面table头工具栏添加按钮请求接口  即物资添加页面
        /// </summary>
        /// <returns></returns>
        public ActionResult AddMaterial() 
        {
            return View();
        }
        /// <summary>
        /// 物资添加页面数据请求接口
        /// </summary>
        /// <param name="listDto">数据模型</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddMaterial(GetMaterialListDto listDto)
        {
            IMaterialManager manager = new MaterialManager();
            manager.AddOneMaterialData(listDto);
            return Json(new { code = 0, msg = "新增成功" });
        }
        /// <summary>
        /// 消防及电梯物资页面table编辑按钮页面请求接口   即物资编辑页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult EditMaterial(int id)
        {
            IMaterialManager manager = new MaterialManager();

            var data = manager.GetMaterialById(id);

            return View(data);
        }
        /// <summary>
        /// 物资编辑页面数据请求接口
        /// </summary>
        /// <param name="listDto">数据模型</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult EditMaterial(GetMaterialListDto listDto)
        {
            IMaterialManager manager = new MaterialManager();
            manager.EditOneMaterialByDto(listDto);
            return Json(new { code = 0, msg = "修改成功" });
        }

        /// <summary>
        /// 社区房屋管理页面
        /// </summary>
        /// <returns></returns>
        public ActionResult StoreyList()
        {
            return View();
        }

        /// <summary>
        /// 社区房屋管理页面table数据请求接口
        /// </summary>
        /// <param name="limit">条数</param>
        /// <param name="page">页数</param>
        /// <param name="issueNumber">期号筛选器</param>
        /// <param name="seatNunber">座号筛选器</param>
        /// <returns></returns>
        public JsonResult GetStoreyList(int limit, int page, string issueNumber, string seatNunber)
        {
            IMaterialManager manager = new MaterialManager();
            int datalet;
            var data = manager.GetAllStoreyListDtos(limit, page, out datalet, issueNumber, seatNunber);
            return Json(new { code = 0, msg = "获取成功", count = datalet, data = data }, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 社区房屋管理页面添加按钮业务请求接口  也是房屋添加页面
        /// </summary>
        /// <returns></returns>
        public ActionResult AddStorey() 
        {
            return View();
        }
        /// <summary>
        /// 房屋添加页面业务请求接口  新增一条房屋信息
        /// </summary>
        /// <param name="listDto">数据模型</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddStorey(GetAllStoreyListDto listDto)
        {
            IMaterialManager manager = new MaterialManager();
            manager.AddOneStoreyByDto(listDto);
            return Json(new { code = 0, msg = "新增成功" });
        }
        /// <summary>
        /// 社区房屋管理页面table表删除按钮业务请求接口  删除一条房屋信息
        /// </summary>
        /// <param name="id">房屋ID</param>
        /// <returns></returns>
        public JsonResult DelStorey(int id) 
        {
            IMaterialManager manager = new MaterialManager();
            manager.DelOneStoreyById(id);
            return Json(new { code = 0, msg = "删除成功" }, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 社区房屋管理页面table编辑按钮业务请求接口  也是房屋修改页面
        /// </summary>
        /// <param name="id">房屋ID</param>
        /// <returns></returns>
        public ActionResult EditStorey(int id)
        {
            IMaterialManager manager = new MaterialManager();
            var data = manager.GetOneStoreyById(id);
            return View(data);
        }
        /// <summary>
        /// 房屋修改页面业务请求接口  修改房屋信息
        /// </summary>
        /// <param name="listDto">数据模型</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult EditStorey(GetAllStoreyListDto listDto)
        {
            IMaterialManager manager = new MaterialManager();
            manager.EditOneStoreyByDto(listDto);
            return Json(new { code = 0, msg = "新增成功" });
        }
        /// <summary>
        /// 公告列表页面
        /// </summary>
        /// <returns></returns>
        public ActionResult PublicityList() 
        {
            return View();
        }
        /// <summary>
        /// 公告列表页面Table数据获取接口
        /// </summary>
        /// <param name="limit">条数</param>
        /// <param name="page">页数</param>
        /// <param name="publicityName">公告标题筛选器</param>
        /// <param name="publicityArticle">公告内容筛选器</param>
        /// <param name="releaseStatus">公告发布状态筛选器</param>
        /// <returns></returns>
        public JsonResult GetPublicityList(int limit, int page, string publicityName, string publicityArticle, string releaseStatus)
        {
            IMaterialManager manager = new MaterialManager();
            int datalet;
            var data = manager.GetPublicityListDtos(limit, page, out datalet, publicityName, publicityArticle, releaseStatus);
            return Json(new { code = 0, msg = "获取成功", count = datalet, data = data }, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 公告列表页面table发布按钮业务请求接口
        /// </summary>
        /// <param name="id">公告ID</param>
        /// <returns></returns>
        public JsonResult ReleasePublicity(int id)
        {
            IMaterialManager manager = new MaterialManager();
            manager.ReleaseOnePublicityById(id);
            return Json(new { code = 0, msg = "发布成功" }, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 公告添加页面
        /// </summary>
        /// <returns></returns>
        public ActionResult AddPublicity()
        {
            return View();
        }

        [HttpPost]
        public JsonResult imgUpload()
        {
            var files = System.Web.HttpContext.Current.Request.Files;
            var file = files[0];
            string filename = file.FileName.Replace(".","-"+DateTime.Now.Ticks +".");//取得文件名并重命名
            string url = @"/img/" + filename;
            string path = Server.MapPath(url);
            file.SaveAs(path);
            return Json(new { code = 0, msg = "上传成功", data = url });
        }
        /// <summary>
        /// 公告添加页面业务请求接口
        /// </summary>
        /// <param name="listDto">数据模型</param>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public JsonResult AddPublicity(GetPublicityListDto listDto)
        {
            IMaterialManager manager = new MaterialManager();
            manager.AddOnePublicityByDto(listDto);
            return Json(new { code = 0, msg = "添加成功" });
        }
        /// <summary>
        /// 公告列表页面table删除按钮业务请求接口
        /// </summary>
        /// <param name="id">公告ID</param>
        /// <returns></returns>
        public JsonResult DelPublicity(int id)
        {
            IMaterialManager manager = new MaterialManager();
            manager.DelOnePublicityById(id);
            return Json(new { code = 0, msg = "删除成功" }, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 公告编辑页面
        /// </summary>
        /// <param name="id">公告ID</param>
        /// <returns></returns>
        public ActionResult EditPublicity(int id) 
        {
            IMaterialManager manager = new MaterialManager();
            var data = manager.GetOnePublicityById(id);
            return View(data);
        }
        /// <summary>
        /// 公告修改页面的业务请求接口
        /// </summary>
        /// <param name="listDto">数据模型</param>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public JsonResult EditPublicity(GetPublicityListDto listDto)
        {
            IMaterialManager manager = new MaterialManager();
            manager.EditOnePublicityByDto(listDto);
            return Json(new { code = 0, msg = "修改成功" });
        }
        /// <summary>
        /// 公告页详情查看
        /// </summary>
        /// <param name="id">公告ID</param>
        /// <returns></returns>
        public ActionResult CheckOnePublicity(int id)
        {
            IMaterialManager manager = new MaterialManager();
            var data = manager.GetOnePublicityById(id);
            if(data.releaseStatus == "False")
            {
                return View(); // 这个可以选择跳转到错误页面，但还没去实现
            }
            return View(data);
        }


        public ActionResult NoticeList(string publicityName)
        {
            IMaterialManager manager = new MaterialManager();
            var data = manager.GetAllPublicityListDtos(publicityName);
            ViewBag.data = data;
            return View();
        }



    }
}